package com.example.demo.config;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.connection.ClusterType;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

import java.util.Arrays;

import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;

@Configuration
public class MongoConfig {

    @Autowired
    MongoSettings mongoSettings;

    public @Bean
    MongoClientFactoryBean mongo() {
        CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
        CodecRegistry pojoCodecRegistry = fromRegistries(
                MongoClientSettings.getDefaultCodecRegistry(),
                fromProviders(pojoCodecProvider));
        MongoClientFactoryBean mongo = new MongoClientFactoryBean();
        MongoCredential credential=MongoCredential.createCredential(mongoSettings.getUsername(),mongoSettings.getDb(),mongoSettings.getPass().toCharArray());
        //can not add credential in this way
        MongoClientSettings setting=MongoClientSettings.builder()
                .credential(credential)
                .codecRegistry(pojoCodecRegistry)
                .build();
        mongo.setHost(mongoSettings.getIpAddress());
        mongo.setPort(mongoSettings.getPort());
        mongo.setCredential(new MongoCredential[]{credential});
        mongo.setMongoClientSettings(setting);
        return mongo;
    }

    @Bean
    public MongoTemplate mongoOperations(MongoClient client){
        return new MongoTemplate(new SimpleMongoClientDatabaseFactory(client, "test"));
    }

    /*@Bean
    public  MongoClient rawClient(){
        MongoCredential credential=MongoCredential.createCredential(mongoSettings.getUsername(),mongoSettings.getDb(),mongoSettings.getPass().toCharArray());
//        ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017");
        MongoClientSettings setting=MongoClientSettings.builder()
                .credential(credential)
//                .applyConnectionString(connectionString)
                .applyToClusterSettings(builder ->
                        builder.hosts(Arrays.asList(new ServerAddress(mongoSettings.getIpAddress(), mongoSettings.getPort())))
                        .mode(ClusterConnectionMode.SINGLE)
                        .requiredClusterType(ClusterType.STANDALONE)
                ).build();
        return MongoClients.create(setting);
    }*/
}
